package neu.soft.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import neu.soft.constant.PayableOrderType;
import neu.soft.entity.FinancialPurchaseOrder;
import neu.soft.entity.PayableOrder;
import neu.soft.exception.AppException;
import neu.soft.exception.AppExceptionCodeMsg;
import neu.soft.mapper.PayableOrderMapper;
import neu.soft.query.BasePageQuery;
import neu.soft.service.PayableOrderService;
import neu.soft.util.SnowflakeIdWorker;
import neu.soft.util.TypeConvert;
import neu.soft.vo.PageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

@Service
@Slf4j
public class PayableOrderServiceImpl implements PayableOrderService {

    @Autowired
    PayableOrderMapper payableOrderMapper;

    private static final DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");


    @Override
    public void addPayableOrder(PayableOrder payableOrder) {

        int res = payableOrderMapper.addPayableOrder(payableOrder);
        if (res < 1) {
            throw new AppException(AppExceptionCodeMsg.DZYJ_NGDZ);
        }
    }

    @Override
    public PayableOrder findById(Long payableOrderId) {

        return payableOrderMapper.findById(payableOrderId);
    }

    @Override
    public PageBean<PayableOrder> findByPage(BasePageQuery query) {

        PageBean<PayableOrder> pageBean = new PageBean<>();
        PageHelper.startPage(query.getPageNow(), query.getPageSize());
        List<PayableOrder> payableOrderList = payableOrderMapper.findByPage(query);
        payableOrderList.forEach(payableOrder -> {
            String type = TypeConvert.enumToString(PayableOrderType.class, payableOrder.getPayType());
            payableOrder.setPayType(type);
        });

        PageInfo<PayableOrder> pageInfo = new PageInfo<>(payableOrderList);
        pageInfo.setTotal(payableOrderList.size());
        pageBean.setTotalRows(pageInfo.getTotal());
        pageBean.setTotalPages(pageInfo.getPages());
        pageBean.setRowData(pageInfo.getList());
        return pageBean;
    }

    @Override
    public PayableOrder generate(FinancialPurchaseOrder financialPurchaseOrder, String payType) {
        Integer financialOrderId = financialPurchaseOrder.getFinancialOrderId();
        BigDecimal totalCost = financialPurchaseOrder.getPurchasePrice();
        //创建日期时间，格式化
        String localDateTime = LocalDateTime.now().format(dateTimeFormatter);
        LocalDateTime parse = LocalDateTime.parse(localDateTime, dateTimeFormatter);
        //创建新应付单
        PayableOrder payableOrder = new PayableOrder();
        //设置字段
        payableOrder.setPayableCost(totalCost);
        payableOrder.setCreatedDate(parse);
        payableOrder.setFinancialOrderId(financialOrderId);
        payableOrder.setPayType(payType);
        //生成应付单Id
        SnowflakeIdWorker idWorker = SnowflakeIdWorker.getInstance();
        long id = idWorker.nextId();
        payableOrder.setPayableOrderId(id);//插入生成的id
        return payableOrder;
    }

    //批量查询应付单
    @Override
    public List<PayableOrder> batchFindPayableOrder(List<Long> payableOrderList) {
        if (payableOrderList == null || payableOrderList.size() == 0) {
            throw new AppException(AppExceptionCodeMsg.REQUEST_PARAM_EMPTY);
        }
        return payableOrderMapper.batchFindPayableOrder(payableOrderList);
    }

    @Override
    public void update(List<PayableOrder> payableOrderList) {
        int res = payableOrderMapper.update(payableOrderList);
        if (res < 1) {
            throw new AppException(AppExceptionCodeMsg.YYDZ_JDWGXSB);
        }
    }

}
